
package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.constant.WareConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.PurchaseDetailDao;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                new QueryWrapper<PurchaseDetailEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        /*
        * key:
status:
wareId: 1*/
        String key = (String) params.get("key");
        if(StringUtils.isNotBlank(key)){
            queryWrapper.lambda().and((wrapper)->{
                wrapper.eq(PurchaseDetailEntity::getPurchaseId,key)
                        .or().eq(PurchaseDetailEntity::getSkuId,key);
            });
        }

        String status = (String) params.get("status");
        if(StringUtils.isNotBlank(status)){
            queryWrapper.lambda().eq(PurchaseDetailEntity::getStatus,status);
        }

        String wareId = (String) params.get("wareId");
        if(StringUtils.isNotBlank(wareId)){
            if(Long.parseLong(wareId) != 0) {
                queryWrapper.lambda().eq(PurchaseDetailEntity::getWareId, wareId);
            }
        }


        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper

        );

        return new PageUtils(page);
    }

    @Override
    public List<PurchaseDetailEntity> listByPurchaseIds(List<Long> ids) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(PurchaseDetailEntity::getPurchaseId,ids)
                .and((wrapper)->{
                    wrapper.eq(PurchaseDetailEntity::getStatus, WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode())
                            .or()
                            .eq(PurchaseDetailEntity::getStatus,WareConstant.PurchaseDetailStatusEnum.CREATED.getCode());
                });
        return this.list(queryWrapper);
    }

}